package com.xnfh.field.viewmodel

import com.xnfh.common.bean.CropBean
import com.xnfh.common.bean.FieldDetailBean
import com.xnfh.common.bean.FieldJfJy
import com.xnfh.common.bean.FieldListBean
import com.xnfh.common.bean.FieldWeatherBean
import com.xnfh.framework.base.BaseViewModel
import com.xnfh.framework.bean.RespStateData
import org.json.JSONObject

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/24 16:53
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/24 16:53
 * @UpdateRemark: 更新说明
 */
class FieldViewModel: BaseViewModel() {

    private val fieldViewModelRes = FieldViewModelRes()

    val fieldData = RespStateData<FieldListBean>()
    val fieldDetailData = RespStateData<FieldDetailBean>()
    val fieldWeather = RespStateData<FieldWeatherBean>()
    val fieldJfJy = RespStateData<FieldJfJy>()


    /**
     * 获取田块列表
     */
    fun requestField() = launch {
        val jsonObject = JSONObject()
        jsonObject.put("current","1")
        jsonObject.put("size","1")
        fieldViewModelRes.requestField(creatRequestBody(jsonObject.toString()),fieldData)
    }

    /**
     * 获取田块详情
     */
    fun requestFieldDetail(fieldId: String) =launch {
        val jsonObject = JSONObject()
        jsonObject.put("fieldId",fieldId)
        fieldViewModelRes.requestFieldDetail(creatRequestBody(jsonObject.toString()),fieldDetailData)
    }

    /**
     * 获取温度、湿度等信息
     */
    fun requestFielsWeather(lonLats: String?) = launch {
        if(lonLats == null) return@launch
        fieldViewModelRes.requestWeather(lonLats,fieldWeather)
    }

    /**
     * 获取积温、积雨
     */
    fun requestFiledJwJy(lonLats: String?) = launch {
        if(lonLats == null) return@launch
        fieldViewModelRes.requestFiledJwJy(lonLats,fieldJfJy)
    }
}